window.onload = function () {
    const { createEditor, createToolbar } = window.wangEditor

    const editorConfig = {
        placeholder: '请输入内容...',
        onChange(editor) {
            const html = editor.getHtml()
            console.log('editor content', html)
            // 也可以同步到 <textarea>
            // 实时同步编辑器内容到隐藏输入框
            $('#blog-content').val(html)
        },
    }

    const editor = createEditor({
        selector: '#editor-container',
        html: '<p><br></p>',
        config: editorConfig,
        mode: 'default', // or 'simple'
    });

    const toolbarConfig = {}

    const toolbar = createToolbar({
        editor,
        selector: '#toolbar-container',
        config: toolbarConfig,
        mode: 'default', // or 'simple'
    })

    $("#submit-btn").click(function (event) {
        // 阻止按钮的默认行为，不走表单发送数据
        event.preventDefault();
        // name不是全局的，id是全局的，也更快
        const formData = {
            title: $('input[name="title"]').val(),
            content: html,
            category_id: $('#category-select').val(),
            csrfmiddlewaretoken: $('input[name="csrfmiddlewaretoken"]').val()
        }

        $.ajax({
            url: '',
            type: 'POST',
            data: formData,
            success: function (res) {
                if (res.code === 200) {
                    alert('发布成功!')
                    window.location.href = '/'
                } else {
                    alert(res.message)
                }
            }
        })
    }
    )
};